package com.example.dynamicschedule.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.dynamicschedule.base.Constant;
import com.example.dynamicschedule.bean.*;
import com.example.dynamicschedule.dao.CkCreditOutCompanyDataMapper;
import com.example.dynamicschedule.dao.CkCreditOutCompanyDataRelationMapper;
import com.example.dynamicschedule.dao.CkCreditOutWuReservedDataMapper;
import com.example.dynamicschedule.service.CkCreditCivilCirminalCasesService;
import com.example.dynamicschedule.service.CkCreditOutCompanyDataService;
import com.example.dynamicschedule.service.CkCreditOutOneReservedDataService;
import com.example.dynamicschedule.service.CkOutsourcingCompanyService;
import com.example.dynamicschedule.utils.DateUtils;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Service;


import java.util.*;


@Service
public class CkCreditOutCompanyDataServiceImpl implements CkCreditOutCompanyDataService {
    protected static final Logger logger = Logger.getLogger(CkCreditOutCompanyDataServiceImpl.class);
    @Autowired
    private CkCreditCivilCirminalCasesService ckCreditCivilCirminalCasesService;
    @Autowired
    private CkCreditOutOneReservedDataService ckCreditOutOneReservedDataService;
    @Autowired
    private CkOutsourcingCompanyService ckOutsourcingCompanyService;
    @Autowired
    private CkCreditOutCompanyDataMapper ckCreditOutCompanyDataMapper;
    @Autowired
    private CkCreditOutCompanyDataRelationMapper ckCreditOutCompanyDataRelationMapper;
    @Autowired
    private CkCreditOutWuReservedDataMapper ckCreditOutWuReservedDataMapper;
    @Value("${firstCode}")
    private String firstCode;

    @Override

    public Map<String, Object> checkOutData(Map<String, List<CkCreditOutCompanyData>> listMap) {
        logger.info("-----委外数据处理---start---param:" + JSON.toJSONString(listMap));
        long startTime = System.currentTimeMillis();
        //处理民事刑事预留
        // Map<String, List<CkCreditOutCompanyData>> civilMapList = checkCivildata(listMap);
        //处理上传预留
        //  Map<String, List<CkCreditOutCompanyData>> recoveryData = checkRecoverydata(civilMapList);
        //自动分配
        //  List<CkCreditOutCompanyData> ckCreditOutCompanyDataList = checkAution(recoveryData);
        // ckCreditOutCompanyDataMapper.insertBatchList(ckCreditOutCompanyDataList);
        listMap.clear();
        //recoveryData.clear();
        // civilMapList.clear();
        long endTime = System.currentTimeMillis();
        logger.info("-----委外数据处理---end---UseTime:" + (endTime - startTime) + " result:");
        return null;
    }

    /**
     * 统计委外公司数据量
     *
     * @param batchNumber
     * @return
     */
    @Override
    public List<Map<String, Object>> allotOutCompanyData(String batchNumber) {
        List<Map<String, String>> outDataRelation = ckCreditOutCompanyDataMapper.getOutDataRelation(batchNumber);
        if (outDataRelation.size() == 0) {
            return null;
        }
        logger.info(JSON.toJSONString("-----统计委外公司数据结果：" + outDataRelation));
        List<CkCreditOutCompanyDataRelation> ckCreditOutCompanyDataRelations = new ArrayList<>();
        for (int i = 0; i < outDataRelation.size(); i++) {
            CkCreditOutCompanyDataRelation ckCreditOutCompanyDataRelation = new CkCreditOutCompanyDataRelation();
            ckCreditOutCompanyDataRelation.setAnjianCount(Integer.valueOf(outDataRelation.get(i).get("allCount")));
            Date date = new Date();
            ckCreditOutCompanyDataRelation.setAllotDate(new Date(date.getTime()));
            ckCreditOutCompanyDataRelation.setBatchNumber(batchNumber);
            ckCreditOutCompanyDataRelation.setOutsourcingCompany(String.valueOf(outDataRelation.get(i).get("out_company_name")));
            ckCreditOutCompanyDataRelation.setCheckModel(String.valueOf(outDataRelation.get(i).get("check_model")));
            ckCreditOutCompanyDataRelation.setFileName(batchNumber + String.valueOf(outDataRelation.get(i).get("out_company_name")));
            ckCreditOutCompanyDataRelation.setAnjianState("");
            ckCreditOutCompanyDataRelations.add(ckCreditOutCompanyDataRelation);
        }
        ckCreditOutCompanyDataRelationMapper.insertBatchList(ckCreditOutCompanyDataRelations);
        return null;
    }
//---------------------------------------     二次修改------------------------------

    private List<CkCreditOutCompanyData> allotCompanyData(Map<String, List<CkCreditOutCompanyData>> map, List<CkCreditOutCompanyData> autionListOk) {
        if (null == map || map.size() == 0) {
            return autionListOk;
        }
        for (Map.Entry<String, List<CkCreditOutCompanyData>> mapPo : map.entrySet()) {
            //当前机构下的所有符合条件的委外公司的权重值
            List<Map<String, Object>> companyPowerValue = ckOutsourcingCompanyService.getOutCompanyPowerValue("2", null, mapPo.getKey(), null);
            logger.info("-----公司权重值结果：" + JSON.toJSONString(companyPowerValue));
            List<CkCreditOutCompanyData> autionList = mapPo.getValue();
            int allCount = autionList.size();
            if (null == companyPowerValue || companyPowerValue.size() == 0) {
                //查询总行下的委外公司权重值
                List<Map<String, Object>> companyTotalPowerValue = ckOutsourcingCompanyService.getOutCompanyPowerValue("2", null, firstCode, null);
                if (companyPowerValue == null || companyTotalPowerValue.size() == 0) {
                    autionListOk.addAll(autionList);
                    logger.info("-----无权重值自动分配后的结果：" + JSON.toJSONString(autionListOk));
                    continue;
                }
                logger.info("-----总公司权重值结果：" + JSON.toJSONString(companyTotalPowerValue));
                for (int i = 0; i < companyTotalPowerValue.size(); i++) {
                    double powerValue = Double.valueOf(String.valueOf(companyTotalPowerValue.get(i).get("power_value"))).doubleValue();
                    int allotCount = (int) Math.ceil(powerValue * Double.valueOf(allCount));
                    int count = 0;
                    Iterator<CkCreditOutCompanyData> ckCreditOutCompanyDataIterator = autionList.iterator();
                    while (ckCreditOutCompanyDataIterator.hasNext()) {
                        count++;
                        CkCreditOutCompanyData ckCreditOutCompanyDataRequire = ckCreditOutCompanyDataIterator.next();
                        ckCreditOutCompanyDataRequire.setOutCompanyName(String.valueOf(companyTotalPowerValue.get(i).get("outsourcing_company")));
                        ckCreditOutCompanyDataRequire.setAllotType("1");
                        ckCreditOutCompanyDataRequire.setCurrentDate(new Date(new Date().getTime()));
                        ckCreditOutCompanyDataRequire.setAllotDate(new Date(new Date().getTime()));
                        ckCreditOutCompanyDataRequire.setRecallStatue("1");
                        ckCreditOutCompanyDataRequire.setCallbackConfirm("1");
                        autionListOk.add(ckCreditOutCompanyDataRequire);
                        ckCreditOutCompanyDataIterator.remove();
                        if (allotCount == count) {
                            break;
                        }
                    }
                }
            } else {
                for (int i = 0; i < companyPowerValue.size(); i++) {
                    double powerValue = Double.valueOf(String.valueOf(companyPowerValue.get(i).get("power_value"))).doubleValue();
                    int allotCount = (int) Math.ceil(powerValue * Double.valueOf(allCount));
                    int count = 0;
                    Iterator<CkCreditOutCompanyData> ckCreditOutCompanyDataIterator = autionList.iterator();
                    while (ckCreditOutCompanyDataIterator.hasNext()) {
                        count++;
                        CkCreditOutCompanyData ckCreditOutCompanyDataRequire = ckCreditOutCompanyDataIterator.next();
                        ckCreditOutCompanyDataRequire.setOutCompanyName(String.valueOf(companyPowerValue.get(i).get("outsourcing_company")));
                        ckCreditOutCompanyDataRequire.setAllotType("1");
                        ckCreditOutCompanyDataRequire.setCurrentDate(new Date(new Date().getTime()));
                        ckCreditOutCompanyDataRequire.setAllotDate(new Date(new Date().getTime()));
                        ckCreditOutCompanyDataRequire.setRecallStatue("1");
                        ckCreditOutCompanyDataRequire.setCallbackConfirm("1");
                        autionListOk.add(ckCreditOutCompanyDataRequire);
                        ckCreditOutCompanyDataIterator.remove();
                        if (allotCount == count) {
                            break;
                        }
                    }
                }
            }
        }
        logger.info("-----自动分配后的结果：" + JSON.toJSONString(autionListOk));
        return autionListOk;
    }


    /**
     * 处理大数据分配过来的委外数据
     *
     * @param outDataMap 大数据分配过来的委外数据 卡种 逾期等级 数据集合
     */
    @Override
    public void allotOutCompanyData(Map<String, Map<String, List<CkCreditOutCompanyData>>> outDataMap) {
        logger.info("----------处理委外数据start---params:" + JSON.toJSONString(outDataMap));
        long startTime = System.currentTimeMillis();
        if (null == outDataMap || outDataMap.size() == 0) {
            return;
        }
        List<CkCreditOutCompanyData> listOk = new ArrayList<>();
        //需要分配的所有分行下的委外数据
        Map<String, List<CkCreditOutCompanyData>> freeEndMap = new HashMap<>();
        for (Map.Entry<String, Map<String, List<CkCreditOutCompanyData>>> mapEntry : outDataMap.entrySet()) {
            //处理民事刑事预留
            checkCivilData(mapEntry.getKey(), mapEntry.getValue());
            //处理上传预留
            checkRecoveryData(mapEntry.getKey(), mapEntry.getValue());
            //处理指定委外公司数据
            Map<String, Object> resultMap = handleAution(mapEntry.getKey(), mapEntry.getValue());
            if (null != resultMap && resultMap.size() > 0) {
                List<CkCreditOutCompanyData> resultList = (List<CkCreditOutCompanyData>) resultMap.get("ok");
                if (null != resultList && resultList.size() > 0) {
                    listOk.addAll(resultList);
                }
                Map<String, List<CkCreditOutCompanyData>> fenMap = (Map<String, List<CkCreditOutCompanyData>>) resultMap.get("free");
                if (null != fenMap && fenMap.size() > 0) {
                    for (Map.Entry<String, List<CkCreditOutCompanyData>> mapEntr : fenMap.entrySet()) {
                        if (null != freeEndMap.get(mapEntr.getKey()) && freeEndMap.get(mapEntr.getKey()).size() > 0) {
                            List<CkCreditOutCompanyData> allList = mapEntr.getValue();
                            boolean flag = freeEndMap.get(mapEntr.getKey()).addAll(allList);
                            if (flag)
                                freeEndMap.put(mapEntr.getKey(), freeEndMap.get(mapEntr.getKey()));
                        } else {
                            freeEndMap.put(mapEntr.getKey(), mapEntr.getValue());
                        }
                    }
                }
            }
        }
        if (freeEndMap.size() > 0) {
            listOk = allotCompanyData(freeEndMap, listOk);
        }
        ckCreditOutCompanyDataMapper.insertBatchList(listOk);
        long endTime = System.currentTimeMillis();
        logger.info("-----委外数据处理---end---UseTime:" + (endTime - startTime) + " result:" + JSON.toJSONString(listOk));

    }

    /***
     *  处理刑事民事案件数据
     * @param listMap
     * @return
     */
    private Map<String, List<CkCreditOutCompanyData>> checkCivilData(String key, Map<String, List<CkCreditOutCompanyData>> listMap) {
        logger.info("-----处理司法预留的委外数据---start");
        Map<String, String> map = new HashMap<>();
        String batch = DateUtils.getBatchNumber();
        String stringDate = batch.substring(6, 8);
        //民事刑事预留15号
        if (Integer.valueOf(stringDate) == 15) {
            map.put("batchNumber", Constant.civilWuBatch);
        } else {
            map.put("batchNumber", Constant.civilOneBatch);
        }
        map.put("caseType", key);
        List<CkCreditCivilCirminalCases> civilList = ckCreditCivilCirminalCasesService.getCivilListByCaseType(map);
        listMap = handCivilList(listMap, civilList);
        civilList.clear();
        logger.info("-----处理司法预留的委外数据---end");
        return listMap;
    }

    /**
     * 处理  大数据分配的委外数据分配民事刑事案件委外公司
     *
     * @param
     * @param
     * @return
     */
    private Map<String, List<CkCreditOutCompanyData>> handCivilList(Map<String, List<CkCreditOutCompanyData>> civilMap, List<CkCreditCivilCirminalCases> ckCreditCivilCirminalCasesList) {
        if (null == ckCreditCivilCirminalCasesList || ckCreditCivilCirminalCasesList.size() == 0 || null == civilMap || civilMap.size() == 0) {
            return civilMap;
        }
        for (Map.Entry<String, List<CkCreditOutCompanyData>> map : civilMap.entrySet()) {
            for (CkCreditOutCompanyData ckCreditOutCompanyData : map.getValue()) {
                for (CkCreditCivilCirminalCases ckCreditCivilCirminalCases : ckCreditCivilCirminalCasesList) {
                    if (ckCreditCivilCirminalCases.getCardNumber().equals(ckCreditOutCompanyData.getCardNumber())) {
                        ckCreditOutCompanyData.setOutCompanyName(ckCreditCivilCirminalCases.getOutCompanyName());
                        ckCreditOutCompanyData.setZhiInstitution(ckCreditCivilCirminalCases.getZhiInstitution());
                        ckCreditOutCompanyData.setFenInstitution(ckCreditCivilCirminalCases.getFenInstitution());
                        ckCreditOutCompanyData.setAllotType("5");
                        ckCreditOutCompanyData.setCurrentDate(new Date(new Date().getTime()));
                        ckCreditOutCompanyData.setAllotDate(new Date(new Date().getTime()));
                        ckCreditOutCompanyData.setRecallStatue("1");
                        ckCreditOutCompanyData.setCallbackConfirm("1");
                    }
                }
            }

        }

        return civilMap;
    }

    /**
     * 处理预留数据
     *
     * @param listMap
     * @return
     */
    private Map<String, List<CkCreditOutCompanyData>> checkRecoveryData(String key, Map<String, List<CkCreditOutCompanyData>> listMap) {
        logger.info("----上传预留的委外数据----start");
        if (listMap == null || listMap.size() == 0) {
            return null;
        }
        String batch = DateUtils.getBatchNumber();
        String stringDate = batch.substring(6, 8);
        Map<String, String> batchCase = new HashMap<>();
        batchCase.put("batchNumber", DateUtils.getBatchNumber());
        batchCase.put("caseType", key);
        if (!stringDate.equals("15")) {
            //一号预留的数据
            List<CkCreditOutOneReservedData> oneList = ckCreditOutOneReservedDataService.getCurrentMethodRecoryData(batchCase);
            listMap = checkOneRecoveryData(listMap, oneList);
        } else {
            //十五预留数据
            List<CkCreditOutWuReservedData> wuList = ckCreditOutWuReservedDataMapper.getCurrentMethodRecoryDate(batchCase);
            listMap = checkWuRecoveryData(listMap, wuList);

        }
        logger.info("----上传预留的委外数据----end");
        return listMap;
    }


    /**
     * 处理  大数据分配的委外数据分配一号预留案件委外公司
     *
     * @param
     * @param
     * @return
     */
    private Map<String, List<CkCreditOutCompanyData>> checkOneRecoveryData(Map<String, List<CkCreditOutCompanyData>> oneRecoveryMap, List<CkCreditOutOneReservedData> ckCreditOutOneReservedDataList) {
        if (null == ckCreditOutOneReservedDataList || ckCreditOutOneReservedDataList.size() == 0 || null == oneRecoveryMap || oneRecoveryMap.size() == 0) {
            return oneRecoveryMap;
        }
        for (Map.Entry<String, List<CkCreditOutCompanyData>> mapEntry : oneRecoveryMap.entrySet()) {
            for (CkCreditOutCompanyData ckCreditOutCompanyData : mapEntry.getValue()) {
                for (CkCreditOutOneReservedData ckCreditOutOneReservedData : ckCreditOutOneReservedDataList) {
                    if (ckCreditOutOneReservedData.getCardNumber().equals(ckCreditOutCompanyData.getCardNumber())) {
                        ckCreditOutCompanyData.setOutCompanyName(ckCreditOutOneReservedData.getOutCompanyName());
                        ckCreditOutCompanyData.setAllotType("3");
                        ckCreditOutCompanyData.setCurrentDate(new Date(new Date().getTime()));
                        ckCreditOutCompanyData.setAllotDate(new Date(new Date().getTime()));
                        ckCreditOutCompanyData.setRecallStatue("1");
                        ckCreditOutCompanyData.setCallbackConfirm("1");
                        ckCreditOutCompanyData.setZhiInstitution(ckCreditOutOneReservedData.getZhiInstitution());
                        ckCreditOutCompanyData.setFenInstitution(ckCreditOutOneReservedData.getFenInstitution());
                    }
                }
            }

        }
        ckCreditOutOneReservedDataList.clear();
        return oneRecoveryMap;
    }

    /***
     * 处理  大数据分配的委外数据分配十五号预留案件委外公司
     * @param
     * @param
     * @return
     */
    private Map<String, List<CkCreditOutCompanyData>> checkWuRecoveryData(Map<String, List<CkCreditOutCompanyData>> wuRecoverMap, List<CkCreditOutWuReservedData> ckCreditOutOneReservedDataList) {
        if (null == ckCreditOutOneReservedDataList || ckCreditOutOneReservedDataList.size() == 0 || null == wuRecoverMap || wuRecoverMap.size() == 0) {
            return wuRecoverMap;
        }
        for (Map.Entry<String, List<CkCreditOutCompanyData>> mapdata : wuRecoverMap.entrySet()) {
            for (CkCreditOutCompanyData ckCreditOutCompanyData : mapdata.getValue()) {
                for (CkCreditOutWuReservedData ckCreditOutWuReservedData : ckCreditOutOneReservedDataList) {
                    if (ckCreditOutWuReservedData.getCardNumber().equals(ckCreditOutCompanyData.getCardNumber())) {
                        ckCreditOutCompanyData.setOutCompanyName(ckCreditOutWuReservedData.getOutCompanyName());
                        ckCreditOutCompanyData.setZhiInstitution(ckCreditOutWuReservedData.getZhiInstitution());
                        ckCreditOutCompanyData.setFenInstitution(ckCreditOutWuReservedData.getFenInstitution());
                        ckCreditOutCompanyData.setAllotType("6");
                        ckCreditOutCompanyData.setCurrentDate(new Date(new Date().getTime()));
                        ckCreditOutCompanyData.setAllotDate(new Date(new Date().getTime()));
                        ckCreditOutCompanyData.setRecallStatue("1");
                        ckCreditOutCompanyData.setCallbackConfirm("1");
                    }
                }
            }
        }
        ckCreditOutOneReservedDataList.clear();
        return wuRecoverMap;
    }

    /***
     * 处理自动分配的数据
     * @param listMap
     */

    private Map<String, Object> handleAution(String cardType, Map<String, List<CkCreditOutCompanyData>> listMap) {
        if (listMap == null || listMap.size() == 0) {
            return null;
        }
        //根据逾期等级 组织分行数据
        Map<String, Map<String, List<CkCreditOutCompanyData>>> resultMap = new HashMap<>();


        //已经分配好的集合
        List<CkCreditOutCompanyData> listOk = new ArrayList<>();
        // 委外的数据 组织成 逾期等级 分行 集合 形式的map
        for (Map.Entry<String, List<CkCreditOutCompanyData>> mapEntry : listMap.entrySet()) {
            //根据分行code 区分要自动处理的委外数据集合
            Map<String, List<CkCreditOutCompanyData>> autionMap = new HashMap<>();
            Iterator<CkCreditOutCompanyData> ckCreditOutCompanyDataIteratorTong = mapEntry.getValue().iterator();
            while (ckCreditOutCompanyDataIteratorTong.hasNext()) {
                CkCreditOutCompanyData ckCreditOutCompanyData = ckCreditOutCompanyDataIteratorTong.next();
                if (ckCreditOutCompanyData.getOutCompanyName() == null || "".equals(ckCreditOutCompanyData.getOutCompanyName())) {
                    //需要自动分配公司的集合
                    List<CkCreditOutCompanyData> autionList = autionMap.get(ckCreditOutCompanyData.getFenInstitution());
                    if (null == autionList || autionList.size() == 0) {
                        autionList = new ArrayList<>();
                        autionList.add(ckCreditOutCompanyData);
                        autionMap.put(ckCreditOutCompanyData.getFenInstitution(), autionList);
                    } else {
                        autionList.add(ckCreditOutCompanyData);
                    }
                } else {
                    listOk.add(ckCreditOutCompanyData);
                }
                ckCreditOutCompanyDataIteratorTong.remove();
            }
            resultMap.put(mapEntry.getKey(), autionMap);

        }
        Map<String, Object> resultmap = xiaFaOutData(cardType, resultMap, listOk);
        return resultmap;
    }

    /**
     * @param cardType 卡种
     * @param needMap  需要分配的数据
     * @param listOk   已经分配好的数据
     * @return
     */

    public Map<String, Object> xiaFaOutData(String cardType, Map<String, Map<String, List<CkCreditOutCompanyData>>> needMap, List<CkCreditOutCompanyData> listOk) {
        Map<String, Object> mapResult = new HashMap<>();
        if (null == needMap || needMap.size() == 0) {
            mapResult.put("ok", listOk);
            return mapResult;
        }
        for (Map.Entry<String, Map<String, List<CkCreditOutCompanyData>>> mapEntry : needMap.entrySet()) {
            for (Map.Entry<String, List<CkCreditOutCompanyData>> map : mapEntry.getValue().entrySet()) {
                //根据逾期等级 和 分行机构找 分行下的指定相应委外公司
                List<Map<String, Object>> companyPowerValue = ckOutsourcingCompanyService.getOutCompanyPowerValue("2", cardType, map.getKey(), mapEntry.getKey());
                logger.info("-----公司权重值结果：" + JSON.toJSONString(companyPowerValue));
                int allCount = map.getValue().size();
                if (null == companyPowerValue || companyPowerValue.size() == 0) {
                    //总行指定模式的委外公司
                    List<Map<String, Object>> allCompanyPowerValue = ckOutsourcingCompanyService.getOutCompanyPowerValue("2", cardType, firstCode, mapEntry.getKey());
                    if (null == allCompanyPowerValue || allCompanyPowerValue.size() == 0) {
                        continue;
                    }
                    for (int i = 0; i < allCompanyPowerValue.size(); i++) {
                        double powerValue = Double.valueOf(String.valueOf(allCompanyPowerValue.get(i).get("power_value"))).doubleValue();
                        int allotCount = (int) Math.ceil(powerValue * Double.valueOf(allCount));
                        int count = 0;
                        Iterator<CkCreditOutCompanyData> ckCreditOutCompanyDataIterator = map.getValue().iterator();
                        while (ckCreditOutCompanyDataIterator.hasNext()) {
                            count++;
                            CkCreditOutCompanyData ckCreditOutCompanyDataRequire = ckCreditOutCompanyDataIterator.next();
                            ckCreditOutCompanyDataRequire.setOutCompanyName(String.valueOf(allCompanyPowerValue.get(i).get("outsourcing_company")));
                            ckCreditOutCompanyDataRequire.setAllotType("1");
                            ckCreditOutCompanyDataRequire.setCurrentDate(new Date(new Date().getTime()));
                            ckCreditOutCompanyDataRequire.setAllotDate(new Date(new Date().getTime()));
                            ckCreditOutCompanyDataRequire.setRecallStatue("1");
                            ckCreditOutCompanyDataRequire.setCallbackConfirm("1");
                            ckCreditOutCompanyDataRequire.setAllowType(cardType);
                            listOk.add(ckCreditOutCompanyDataRequire);
                            ckCreditOutCompanyDataIterator.remove();
                            if (allotCount == count) {
                                break;
                            }
                        }
                    }
                } else {
                    for (int i = 0; i < companyPowerValue.size(); i++) {
                        double powerValue = Double.valueOf(String.valueOf(companyPowerValue.get(i).get("power_value"))).doubleValue();
                        int allotCount = (int) Math.ceil(powerValue * Double.valueOf(allCount));
                        int count = 0;
                        Iterator<CkCreditOutCompanyData> ckCreditOutCompanyDataIterator = map.getValue().iterator();
                        while (ckCreditOutCompanyDataIterator.hasNext()) {
                            count++;
                            CkCreditOutCompanyData ckCreditOutCompanyDataRequire = ckCreditOutCompanyDataIterator.next();
                            ckCreditOutCompanyDataRequire.setOutCompanyName(String.valueOf(companyPowerValue.get(i).get("outsourcing_company")));
                            ckCreditOutCompanyDataRequire.setAllotType("1");
                            ckCreditOutCompanyDataRequire.setCurrentDate(new Date(new Date().getTime()));
                            ckCreditOutCompanyDataRequire.setAllotDate(new Date(new Date().getTime()));
                            ckCreditOutCompanyDataRequire.setRecallStatue("1");
                            ckCreditOutCompanyDataRequire.setCallbackConfirm("1");
                            ckCreditOutCompanyDataRequire.setAllowType(cardType);
                            listOk.add(ckCreditOutCompanyDataRequire);
                            ckCreditOutCompanyDataIterator.remove();
                            if (allotCount == count) {
                                break;
                            }
                        }
                    }
                }
            }
        }
        Map<String, List<CkCreditOutCompanyData>> fenMap = new HashMap<>();
        if (null != needMap && needMap.size() > 0) {
            //根据分行下不限卡种分配  整合数据结构   分行 集合
            for (Map.Entry<String, Map<String, List<CkCreditOutCompanyData>>> mapEntry : needMap.entrySet()) {
                for (Map.Entry<String, List<CkCreditOutCompanyData>> map : mapEntry.getValue().entrySet()) {
                    if (null == map.getValue() || "".equals(map.getValue())) {
                        continue;
                    }
                    if (null == fenMap.get(map.getKey())) {
                        fenMap.put(map.getKey(), map.getValue());
                    } else {
                        List<CkCreditOutCompanyData> allList = map.getValue();
                        boolean flag = fenMap.get(map.getKey()).addAll(allList);
                        if (flag) {
                            fenMap.put(map.getKey(), fenMap.get(map.getKey()));
                        }
                    }
                }
            }
        }
        mapResult.put("ok", listOk);
        if (fenMap.size() > 0) {
            mapResult.put("free", fenMap);
        }
        return mapResult;
    }
}
